<template>
  <div class="flex flexColumn flex1">
    <div class="title">
      <span>首页 &gt; 商品管理 &gt; 商品列表</span>
    </div>
    <div class="main flex flexColumn">
      <div class="main-title">商品列表</div>
      <el-table :data="list" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props" class="line">
            <el-form label-position="left" inline class="demo-table-expand">
              <div class="flex">
                <el-form-item label="商品ID">
                  <span>{{ props.row.id }}</span>
                </el-form-item>
                <el-form-item label="商品名称">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
              </div>
              <div class="flex">
                <el-form-item label="所属分类">
                  <span>{{ props.row.category }}</span>
                </el-form-item>
                <el-form-item label="商品价格">
                  <span>{{ props.row.price.toFixed(2) }}</span>
                </el-form-item>
              </div>
              <div class="flex">
                <el-form-item label="商品图片">
                  <img :src="props.row.imgUrl" alt="" />
                </el-form-item>
                <el-form-item label="创建时间">
                  <span>{{ props.row.ctime }}</span>
                </el-form-item>
              </div>
              <div class="flex">
                <el-form-item label="商品评价">
                  <span>{{ props.row.rating }}</span>
                </el-form-item>
                <el-form-item label="商品销量">
                  <span>{{ props.row.sellCount }}</span>
                </el-form-item>
              </div>
              <el-form-item label="商品描述">
                <span>{{ props.row.goodsDesc }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" prop="name" width="">
        </el-table-column>
        <el-table-column label="所属分类" prop="category"> </el-table-column>
        <el-table-column label="商品价格">
          <template slot-scope="data">
            {{ data.row.price.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column label="商品图片">
          <template slot-scope="props">
            <img :src="props.row.imgUrl" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="描述" prop="goodsDesc"> </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)" type="light"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="goon"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @current-change="pageFn"
          @size-change="changeSize"
          :page-size="pageSize"
          :page-sizes="[5, 10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-drawer
      title="修改商品"
      :visible.sync="dialog"
      direction="ltr"
      custom-class="demo-drawer"
      ref="drawer"
    >
      <div class="demo-drawer__content">
        <el-form inline label-width="80px" class="flex flexColumn from">
          <el-form-item label="商品名称">
            <el-input v-model="editProObj.name" clearable=""></el-input>
          </el-form-item>
          <div class="flex">
            <p>商品分类</p>
            <el-select v-model="editProObj.category" placeholder="请选择">
              <el-option
                v-for="(item, index) in catelist"
                :key="index"
                :label="item.cateName"
                :value="item.cateName"
              >
              </el-option>
            </el-select>
          </div>
          <div class="flex addpro-price">
            <p>商品价格</p>
            <el-input-number
              v-model="editProObj.price"
              :min="1"
              label="描述文字"
            ></el-input-number>
          </div>
          <div class="flex addpro-img">
            <p>商品图片</p>
            <el-upload
              class="avatar-uploader"
              action="http://127.0.0.1:5000/goods/goods_img_upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img
                v-if="editProObj.imgUrl"
                :src="editProObj.imgUrl"
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
          <el-form-item label="商品描述">
            <el-input type="textarea" v-model="editProObj.goodsDesc"></el-input>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer" style="padding-left: 20px">
          <el-button @click="cancelForm" type="light">取 消</el-button>
          <el-button type="goon" @click="editPro" :loading="loading">{{
            loading ? "提交中 ..." : "确 定"
          }}</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>


<script>
import { goodlist, editgoods, del, categories } from "@/apis/items.js";
import { getYYYYMMDDHHMMSS } from "@/utlis/utils";

export default {
  data() {
    return {
      list: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      imgUrl: "",
      catelist: [],
      dialog: false,
      loading: false,
      editProObj: {},
    };
  },
  created() {
    categories().then((res) => {
      this.catelist = res.data.categories;
    });
    this.reProlist();
  },
  methods: {
    pageFn(page) {
      this.currentPage = page;
      this.reProlist();
    },
    changeSize(size) {
      this.pageSize = size;
      this.reProlist();
    },
    reProlist() {
      goodlist({ currentPage: this.currentPage, pageSize: this.pageSize }).then(
        (res) => {
          for (let obj of res.data.data) {
            obj.ctime = getYYYYMMDDHHMMSS(obj.ctime);
          }
          this.list = res.data.data;
          this.total = res.data.total;
        }
      );
    },
    handleDelete(id) {
      this.$confirm("此操作将永久该账户, 是否继续?", "优选系统提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          del({ id: id }).then((res) => {
            if (res.data.code == 0) {
              this.reProlist();
            }
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleEdit(row) {
      this.dialog = true;
      this.id = row.id;
      this.editProObj = JSON.parse(JSON.stringify(row));
    },
    editPro() {
      let { name, category, price, goodsDesc } = this.editProObj;
      this.imgUrl = this.imgUrl
        ? this.imgUrl
        : this.editProObj.imgUrl.substr(
            this.editProObj.imgUrl.lastIndexOf("/") + 1
          );
      editgoods({
        name,
        category,
        price,
        imgUrl: this.imgUrl,
        goodsDesc,
        id: this.id,
      }).then((res) => {
        console.log(res);
        if (res.data.code == 0) {
          this.reProlist();
          this.loading = false;
          this.dialog = false;
          this.$message({
            type: "success",
            message: "优选管理系统提示:" + this.name + "商品信息修改成功!",
          });
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.imgUrl = res.imgUrl;
      this.editProObj.imgUrl = URL.createObjectURL(file.raw);
    },

    cancelForm() {
      this.loading = false;
      this.dialog = false;
    },
  },
};
</script>

<style lang="less" scoped>
img {
  width: 80px;
  height: 80px;
  border-radius: 13px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.addpro-img {
  padding-bottom: 20px;
}
.addpro-price {
  padding: 20px 0;
}
p {
  font-size: 14px;
  color: #606266;
  padding-right: 12px;
  text-align: right;
  width: 70px;
}
[class*=" el-icon-"],
[class^="el-icon-"] {
  line-height: 178px;
}
.el-button--goon {
  color: #fff;
  background-color: #706c61;
  border-color: #706c61;
}
.el-button--light {
  color: #706c61;
  background-color: #e1f4f3;
  border-color: #e1f4f3;
}
</style>